package com.hdc.hdc.paltform.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.hdc.hdc.paltform.entity.BLImages;
import com.hdc.hdc.paltform.entity.BusinessLocation;
import com.hdc.hdc.paltform.entity.vo.BLIVO;
import com.hdc.hdc.paltform.repository.AdvertBlimagesRespository;
import com.hdc.hdc.paltform.repository.AdvertBusinessLocationRespository;
import com.hdc.hdc.paltform.service.AdvertServiceI;

/**
 * 广告位服务类
 * @author GJY
 * 人生格言：学以致用终将成功
 * @version 创建时间：2018年4月2日 下午8:29:59 
 *
 */
@Service
public class AdvertServiceImpl implements AdvertServiceI {
	
	private static final Logger log = LoggerFactory.getLogger(AdvertServiceImpl.class);
	
	@Autowired
	private AdvertBlimagesRespository advertBlimagesRespository;
	
	@Autowired
	private AdvertBusinessLocationRespository advertBusinessLocationRespository;

	@Override
	public Map<String, Object> getBlivos(Integer currentPage,Integer pasgeSize) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<BLIVO> blivos = new ArrayList<>();
		
		Pageable pb = PageRequest.of(currentPage-1, pasgeSize);
		
		Page<BusinessLocation> blsp = this.advertBusinessLocationRespository.findAll(pb);
		
		map.put("maxPage", blsp.getTotalPages());
		
		List<BusinessLocation> bls = blsp.getContent();
		
		//一个集合只能放四个
		int i = 0;
		
		//一次只能放四个集合
		int j = 0;
		
		for (BusinessLocation bl : bls) {
			
			if (i>3) {
				
				log.info("当前map是第"+(j+1)+"个");
				blivos = new ArrayList<>();
				i=0;
				++j;
			}
		
			BLIVO blivo = new BLIVO();
			BeanUtils.copyProperties(bl, blivo);
			
			List<String> imageUrl = new ArrayList<>();
			
			List<BLImages> bis = this.advertBlimagesRespository.findByBlId(bl.getId());
			
			for (BLImages bi : bis) {
				imageUrl.add(bi.getUrl());
			}
			
			blivo.setImageUrl(imageUrl);
			
			blivos.add(blivo);
			
			map.put(String.valueOf(j), blivos);
			
			i++;
		}
		log.info("map元素个数："+map.size());
		return map;
	}

	@Override
	public Long findAdvertForPage() {
		return this.advertBusinessLocationRespository.count();
	}

}
